
from rest_framework.views import APIView
from rest_framework.permissions import IsAdminUser
from apps.users.models import User
from datetime import date, timedelta
from rest_framework.response import Response
# 统计商品访问量
from apps.goods.models import GoodsVisitCount







# 统计月用户增量
class UserMonetCountView(APIView):
    def get(self, request):
        # 拿到时间
        now_day = date.today()
        # 拿到三十天前的时间
        old_date = now_day - timedelta(30)

        # 通过for循环拿到30天中每一天的用户增量
        count_user = []
        for i in range(0, 31):
            date_addtime = old_date + timedelta(i)
            index_date = old_date + timedelta(i + 1)
            # 拿到这一天的用户增量
            user_count = User.objects.filter(date_joined__gte=date_addtime, date_joined__lt=index_date).count()
            count_user.append({
                    'count': user_count,
                    'date': date_addtime,
            })
        # 返回数据
        return Response(count_user)







# ==================================================================================================
# 统计商品当天的访问量
class GoodsCountView(APIView):
    def get(self, request):
        now_day = date.today()
        goods = GoodsVisitCount.objects.filter(date=now_day)
        goods_list = []
        for good in goods:
            goods_list.append({
                'category': good.category.name,
                'count': good.count
            })
        return Response(goods_list)


# ========================================================================

# 日下单用户
class UserOrderView(APIView):
    def get(self, request):
        '''日下单用户'''
        now_day = date.today()
        # 拿到登录用户数
        user_list = User.objects.filter(orderinfo__create_time__gte=now_day)
        count = len(set(user_list))

        return Response({'count': count,
                         'date': now_day})


# ==================================================================================

# 日活用户
class UserLiveView(APIView):
    def get(self, request):
        '''日活用户'''
        now_day = date.today()
        # 拿到登录用户数
        count = User.objects.filter(last_login__gte=now_day).count()

        return Response({'count': count,
                         'date': now_day})


# ============================================================================================
# 日增用户
class UserTodayAddView(APIView):
    permission_classes = [IsAdminUser]
    def get(self, request):
        '''日增用户'''
        now_day = date.today()
        count = User.objects.filter(date_joined__gte=now_day).count()

        return Response({'count': count,
                         'date': now_day})


# ================================================================================
# 统计用户总数
class UserCountView(APIView):
    # 只有管理员用户才能访问
    permission_classes = [IsAdminUser]

    def get(self, request):
        '''用户总数'''
        # 查询用户总数返回数据
        count = User.objects.filter(is_staff=False).count()

        now_date = date.today()

        # 返回数据
        return Response({
            'count': count,
            'date': now_date,
        })